package com.itheima.pinda.impl.truck;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.pinda.DTO.entity.pojo.truck.PdTruck;
import com.itheima.pinda.DTO.entity.pojo.truck.PdTruckLicense;
import com.itheima.pinda.feign.service.truck.PdTruckLicenseService;
import com.itheima.pinda.feign.service.truck.PdTruckService;
import com.itheima.pinda.mapper.truck.PdTruckLicenseMapper;
import com.itheima.pinda.utils.CustomIdGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 车辆行驶证表 服务实现类
 * </p>
 *
 * @author xy
 * @since 2021-11-15
 */
@Service
public class PdTruckLicenseServiceImpl extends ServiceImpl<PdTruckLicenseMapper, PdTruckLicense> implements PdTruckLicenseService {

    @Autowired
    private CustomIdGenerator idGenerator;

    @Autowired
    private PdTruckService truckService;

    @Override
    public PdTruckLicense saveTruckLicense(PdTruckLicense truckLicense) {

        if (truckLicense.getId() == null) {
           truckLicense.setId(idGenerator.nextId(truckLicense)+"");
            int insert = baseMapper.insert(truckLicense);

//            处理车辆信息中的关联字段
            if (truckLicense.getTruckId() != null){
                PdTruck truck = truckService.getById(truckLicense.getTruckId());
                truck.setTruckLicenseId(truckLicense.getId());
                truckService.updateById(truck);
            }


        } else {
            baseMapper.updateById(truckLicense);
        }
        return truckLicense;
    }
}
